<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\CourseColumnStoreRequest;
use App\Models\CourseColumn;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class CourseColumnController extends AdminBaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse|object
     */
    public function index()
    {
        $list = CourseColumn::filter(\request()->all())->orderByRaw('id desc')->paginate(\request('perPage', 10));
        return $this->restful($list);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CourseColumnStoreRequest $request
     * @return JsonResponse|object
     */
    public function store(CourseColumnStoreRequest $request)
    {
        $params = $request->validated();
        $params['resource_tags'] = [];
        if (!empty($params['resource']['learn_videos'])) $params['resource_tags'][] = '视频课程';
        if (!empty($params['resource']['courses'])) $params['resource_tags'][] = '题库';
        if (!empty($params['resource']['documents'])) $params['resource_tags'][] = '文档';

        // 计算视频课程时长
        if (!empty($params['resource']['learn_videos'])) {
            $learnVideoIds = $params['resource']['learn_videos'];
            $params['section_count'] = \DB::table('learn_videos')->whereIn('id', $learnVideoIds)->sum('section_count');
        }

        $model = CourseColumn::create($params);

        return $this->restful($model);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param CourseColumnStoreRequest $request
     * @param int $id
     * @return JsonResponse|object
     */
    public function update(CourseColumnStoreRequest $request, int $id)
    {
        $params = $request->validated();
        $params['resource_tags'] = [];
        if (!empty($params['resource']['learn_videos'])) $params['resource_tags'][] = '视频课程';
        if (!empty($params['resource']['courses'])) $params['resource_tags'][] = '题库';
        if (!empty($params['resource']['documents'])) $params['resource_tags'][] = '文档';

        // 计算视频课程时长
        if (!empty($params['resource']['learn_videos'])) {
            $learnVideoIds = $params['resource']['learn_videos'];
            $params['section_count'] = \DB::table('learn_videos')->whereIn('id', $learnVideoIds)->sum('section_count');
        }

        $model = CourseColumn::where('id', $id)->update($params);

        return $this->restful($model);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse|object
     */
    public function destroy(int $id)
    {
        $model = CourseColumn::where('id', $id)->delete();

        return $this->restful($model);
    }

    public function getOptions()
    {
        $list = CourseColumn::select(['id as value', 'name as label'])->get()->toArray();
        return $this->restful($list);
    }
}
